$(function () {
    $.ajax({
        url:baseloaction +'/web/index/getIndexPopupSlogan',
        type:'post',
        dataType:'json',
        async:false,
        data:{},
        success: function (result) {
            if (result.success == true) {
                var obj = result.entity;
                var imagUrl = obj.imag;
                var id = obj.popupSloganDetailId;
                var type = obj.chainedType;
                var url = obj.chained;
                diglog(imagUrl, id, type, url);
            }
        }
    });
})

//弹窗广告
function diglog(imagUrl,id,type,url) {
        areaW = '',
        timer = null,
        _ele = '';
        _ele += '<div><img id="adsCurrImg" src="'+imagUrl+'" style="max-width: 600px;max-height: 800px"></div>';
        var imgObj = new Image();
            imgObj.src = imagUrl;
            imgObj.onload = function() {
                console.log('imgObj',imgObj.naturalWidth);
                areaW = imgObj.naturalWidth
            }
    document.onreadystatechange = adsDialogFn;
    function adsDialogFn() {
        clearTimeout(timer);
        if (document.readyState === "complete") {
            timer = setTimeout(function () {
                layer.open({
                    type: 1,
                    title: false,
                    area: areaW + 'px' || '600px',
                    skin: 'layer_ads__wrap',
                    content: _ele,
                    success: function(layero, index){
                        var adsImg = $(layero).find('#adsCurrImg');
                        //1、设置内外部链接时
                        if('1'==type){
                            $(adsImg).wrap('<a href='+url+' onclick="addClick('+id+','+index+')" target="_blank"></a>');
                        }else if('2' == type){
                            //2、设置三方客服时
                            $(adsImg).off().on('click', function () {
                                addClick(id,index);
                                window.open(url,'newwindow', 'toolbar=no,scrollbars=yes,location=no,resizable=no,top=200,left=300,width=800,height=600');
                            })
                        }
                    }
                })
            }, 1500)
        }
    }
};

/**
 * 添加弹框点击
 * @param id
 */
function addClick(id,index) {
    layer.close(index);
    $.ajax({
        url:baseloaction +'/web/index/addClick',
        type:'post',
        dataType:'json',
        async:false,
        data:{"id":id},
        success: function (result) {
        }
    });
}

